package com.hxexp.weixin.mvc.common.service.impl;

import org.joda.time.LocalDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;

import com.hxexp.weixin.common.cache.CalendarCacheManager;
import com.hxexp.weixin.constant.StringConstant;
import com.hxexp.weixin.mvc.common.service.CalendarService;

/**
 * 日历服务实现类
 *
 * @author li.jiang
 *
 */
@Service
public final class CalendarServiceImpl implements CalendarService {
    private static final String CALENDAR_DATA_ATTR = "calendarData";
    private static final String YEAR_ATTR = "year";
    private static final String MONTH_ATTR = "month";
    private static final String LAST_MONTH_ATTR = "lastMonth";
    private static final String NEXT_MONTH_ATTR = "nextMonth";
    private static final String LAST_YEAR_ATTR = "lastYear";
    private static final String NEXT_YEAR_ATTR = "nextYear";
    private static final String DAY_ATTR = "day";
    private static final String TODAY_ATTR = "today";
    private static final String YMD_FORMAT = "yyyy-M-d";

    @Autowired
    private CalendarCacheManager calendarCacheManager;

    @Override
    public void initCalendar(String date, String isRoll, Model model) {
        String[][] calendarData = new String[][] {};
        LocalDate now = LocalDate.now();

        // 出发日期参数为空
        if (StringUtils.isEmpty(date)) {
            calendarData = calendarCacheManager.getCalendarArray(now.getYear(), now.getMonthOfYear());// 获取当前日期的日历数据
            model.addAttribute(CALENDAR_DATA_ATTR, calendarData);
            model.addAttribute(YEAR_ATTR, now.getYear()); // 当前年
            model.addAttribute(MONTH_ATTR, now.getMonthOfYear()); // 当前月
            model.addAttribute(LAST_MONTH_ATTR, now.minusMonths(1).getMonthOfYear()); // 上月
            model.addAttribute(NEXT_MONTH_ATTR, now.plusMonths(1).getMonthOfYear()); // 下月
            model.addAttribute(LAST_YEAR_ATTR, now.minusMonths(1).getYear()); // 上月对应的年
            model.addAttribute(NEXT_YEAR_ATTR, now.plusMonths(1).getYear()); // 下月对应的年
            model.addAttribute(DAY_ATTR, StringConstant.BLANK); // 选中的天
            model.addAttribute(TODAY_ATTR, now.toString(YMD_FORMAT));// 当天
        } else {
            LocalDate selectLeaveDate = new LocalDate(date);
            // 获取选定日期 的日历数据
            calendarData = calendarCacheManager.getCalendarArray(selectLeaveDate.getYear(), selectLeaveDate.getMonthOfYear());
            model.addAttribute(CALENDAR_DATA_ATTR, calendarData);
            model.addAttribute(YEAR_ATTR, selectLeaveDate.getYear());
            model.addAttribute(MONTH_ATTR, selectLeaveDate.getMonthOfYear());
            model.addAttribute(LAST_MONTH_ATTR, selectLeaveDate.minusMonths(1).getMonthOfYear());
            model.addAttribute(NEXT_MONTH_ATTR, selectLeaveDate.plusMonths(1).getMonthOfYear());
            model.addAttribute(LAST_YEAR_ATTR, selectLeaveDate.minusMonths(1).getYear());
            model.addAttribute(NEXT_YEAR_ATTR, selectLeaveDate.plusMonths(1).getYear());

            // 日历未滚动，设置选中的天
            if (StringUtils.isEmpty(isRoll)) {
                model.addAttribute(DAY_ATTR, selectLeaveDate.getDayOfMonth());
            } else {
                model.addAttribute(DAY_ATTR, StringConstant.BLANK);
            }
            // 选中的月和当前月一致，设置表示当天的数字，用于在前端页面将表示今天的数字标红
            if (selectLeaveDate.getMonthOfYear() == now.getMonthOfYear()) {
                model.addAttribute(TODAY_ATTR, now.toString(YMD_FORMAT));
            } else {
                model.addAttribute(TODAY_ATTR, StringConstant.BLANK);
            }
        }
    }
}
